package com.dudu.timer;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import com.dudu.access.ImsUploadFile;
import com.dudu.access.RongLianUtils;
import com.dudu.access.TianYiUtil;
import com.dudu.access.YonYouUtil;
import com.dudu.bean.ResponseSendVoiceBean;
import com.dudu.dao.SendNoticeDao;
import com.dudu.dao.UploadFileDao;
import com.dudu.enumeration.AbilityTypeEnum;
import com.dudu.model.UploadFileModel;
import com.dudu.utils.Encrypt;
import com.dudu.utils.ProperyResource;
import com.dudu.utils.SsoUtil;

import core.CoreDao;

/**
 * 文件上传线程
 *  
 * @author gaoxs
 * 
 */
public class UploadFileThread extends java.lang.Thread{//implements Runnable {
    private static final Logger log = core.util.Logger.getLogger(UploadFileThread.class.getSimpleName());
    private CoreDao dao = null;
    UploadFileModel fileModel = new UploadFileModel();
    private SendNoticeDao mSendNoticeDao = null;
    private UploadFileDao uploadfileDao = null;
    private String ability_type = "";

    public UploadFileThread(CoreDao dao, UploadFileModel fileModel, String ability_type, SendNoticeDao mSendNoticeDao, UploadFileDao uploadfileDao) {
	this.dao = dao;
	BeanUtils.copyProperties(fileModel, this.fileModel);
	this.mSendNoticeDao = mSendNoticeDao;
	this.uploadfileDao = uploadfileDao;
	this.ability_type = ability_type;
    }

    @Override
    public void run() {
	try {
	    log.info("开始向通道上传文件:"+fileModel.getMediaName()+"--ability_type:"+ability_type);
	    ResponseSendVoiceBean responseSendVoice = null;
	    String voice_name = "";
	    String voice_type = "";
	    // 获取文件
	    SsoUtil sso = new SsoUtil();
	    int flag = sso.getNewFile(fileModel.getFileFullName(), fileModel.getFileAddress());
	   
	    if (flag == 1) {
		// 向第三方上传文件
		if (ability_type.equals(AbilityTypeEnum.AUDEOSEND.getKey()) || ability_type.equals(AbilityTypeEnum.IVR_IMS.getKey())) {
		    // ims mp3格式 可带扩展名也可不带
		    Map<String, String> paramMap = new HashMap<String, String>();
		    paramMap.put("Name", fileModel.getMediaName());
		    paramMap.put("Spid", ProperyResource.getSpId());
		    paramMap.put("Appid", ProperyResource.getAppId());
		    paramMap.put("Passwd", Encrypt.sha1Encrypt(ProperyResource.getPassword()));
		    responseSendVoice = ImsUploadFile.post(ProperyResource.getServiceUploadVoiceUrl(), paramMap, fileModel.getFileAddress());
		    voice_name = fileModel.getMediaName();
		    voice_type = "ims";
		}else if(ability_type.startsWith(AbilityTypeEnum.YONYOUAUDEO.getKey()) || ability_type.equals(AbilityTypeEnum.IVR_YONYOU.getKey())){
		    //自建通道
		    String mediaName = fileModel.getMediaName();
		    voice_name=mediaName;
		    responseSendVoice=YonYouUtil.UploadFile(fileModel.getFileFullName());
		    voice_type = "yonyou";
		}else if (ability_type.startsWith(AbilityTypeEnum.RONGLIANAUDEO.getKey()) || ability_type.equals(AbilityTypeEnum.IVR_RONGL.getKey())) {
		    // 荣联 wav格式 必带扩展名
		    String mediaName = fileModel.getMediaName();
		    if (fileModel.getMediaName().indexOf(".") > -1) {
			String t_ext = fileModel.getMediaName().substring(fileModel.getMediaName().lastIndexOf("."));
			voice_name = fileModel.getMediaName().replace(t_ext, ".wav");
			mediaName = fileModel.getMediaName().replace(t_ext, "");
		    } else {
			voice_name = fileModel.getMediaName() + ".wav";
		    }
		    responseSendVoice = RongLianUtils.UploadFile(mediaName, fileModel.getFileAddress());
		    voice_type = "ronglian";
		} else if (ability_type.equals(AbilityTypeEnum.IVR_TianYi.getKey()) || ability_type.contains(AbilityTypeEnum.TIANYIAUDEO.getKey())) {
		    // 天亿 wav 格式 不带扩展名
		    String mediaName = fileModel.getMediaName();
		    if (fileModel.getMediaName().indexOf(".") > -1) {
			String t_ext = fileModel.getMediaName().substring(fileModel.getMediaName().lastIndexOf("."));
			voice_name = fileModel.getMediaName().replace(t_ext, "");
			mediaName = fileModel.getMediaName().replace(t_ext, "");
		    } else {
			voice_name = fileModel.getMediaName();
		    }
		    /**
		     * 是否为青岛项目请求 70字和140字套餐为青岛项目产品
		     */
		    boolean isQingDao = false;
		    if (ability_type.contains("70") || ability_type.contains("140")) {
			isQingDao = true;
		    }
		    responseSendVoice = TianYiUtil.UploadFile(mediaName, fileModel.getFileAddress(), isQingDao);
		    voice_type = "tianyi";
		}else{
		    log.error("没有此通道的上传功能ability_type："+ability_type); 
		}
	    } else {
		log.error("获取文件失败"+fileModel.getMediaName());
		mSendNoticeDao.updateSendVoice(dao, "1", "-1|获取文件失败", fileModel.getMediaName());
	    }

	    // 上传成功
	    if (null != responseSendVoice && responseSendVoice.getResult().equals("0")) {
		uploadfileDao.updateFileInfo(dao, fileModel.getMediaName(), voice_name, voice_type);
		mSendNoticeDao.updateVoice_name(dao, voice_name, fileModel.getMediaName());
		log.info(fileModel.getMediaName()+"上传成功");
	    }

	} catch (Exception e) {
	    e.printStackTrace();
	}

    }

}
